package nc.impl.pub.service;

import java.util.List;
import java.util.Map;

import nc.bs.dao.BaseDAO;
import nc.itf.pub.service.IQuerySubjectService;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.vo.pub.BusinessException;
import nccloud.commons.lang.StringUtils;

public class QuerySubjectServiceImpl implements IQuerySubjectService{
	private BaseDAO baseDAO;

	@Override
	public List<Map<String, Object>> queryAccasoaStartAndEndCodeBypk(String pk_accchart, String acountStart)
			throws BusinessException {
		if("".equals(acountStart)) {
			return null;
		}
		String sql = this.BuildSQLLikeByPK(pk_accchart, acountStart);
		Object obj = this.getBaseDAO().executeQuery(sql, new MapListProcessor());
		if(obj == null) return null;
		
		return (List<Map<String, Object>> )obj;
	}

	@Override
	public Map<String, Object> queryAccasoaCodeBypk(String pk_accchart, String acountCode)
			throws BusinessException {
		String sql = this.BuildSQLByPK(pk_accchart, acountCode);
		Object obj = this.getBaseDAO().executeQuery(sql, new MapProcessor());
		if(obj == null) return null;
		return (Map<String, Object>)obj;
	}

	@Override
	public Map<String, Object> handlerParam(Map<String, Object> param, List<Map<String, Object>> lists) throws BusinessException {
		if(lists == null  || lists.isEmpty()) {
			return param;
		}
		if(param.get("startcode")  == null || param.get("endcode") == null ) {
			param.put("startcode", lists.get(0).get("code"));
			param.put("endcode", lists.get(lists.size()-1).get("code"));
		}
		return param;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> handlerYeAndMXParam(String pk_accchart,Map<String, Object> param)
			throws BusinessException {
		if(param.get("subjectType") == null) {
			return param;
		}
		String sql = this.BuildSQLLikeByPKS(pk_accchart, param.get("subjectType").toString());
		List<String> lists = (List<String>)this.getBaseDAO().executeQuery(sql, new ColumnListProcessor());
		if(lists.isEmpty()) return param;
		if(param.get("qryObjs") == null) return param;
		List<Map<String,Object>> qryObjs = (List<Map<String,Object>>)param.get("qryObjs");
		if(qryObjs.isEmpty()) return param;
		for(Map<String,Object> map : qryObjs ) {
			if("会计科目".equals(map.get("pk_checktype").toString())) {
				String join = StringUtils.join(lists, ",");
				map.put("pk_checkvalue", join);
			}
		}
		return param;
	}
	
	protected String BuildSQLLikeByPK(String pk_accchart, String acountStart) {
        String sql = " select" +
                "     t.code, t.name  " +
                "  from bd_accasoa a, bd_account t " +
                " where a.pk_account = t.pk_account " +
                "   and a.pk_accchart = '" + pk_accchart + "' " +
                "   and t.code like '" + acountStart + "%' " +
                " order by t.code asc";
		return sql;
	}
	

	protected String BuildSQLLikeByPKS(String pk_accchart, String acountStart) {
        String sql = " select" +
                "     a.pk_accasoa  " +
                "  from bd_accasoa a, bd_account t " +
                " where a.pk_account = t.pk_account " +
                "   and a.pk_accchart = '" + pk_accchart + "' " +
                "   and t.code like '" + acountStart + "%' " +
                " order by t.code asc";
		return sql;
	}
	
	protected String BuildSQLByPK(String pk_accchart, String acountCode) {
        String sql = " select" +
                "     t.code, t.name  " +
                "  from bd_accasoa a, bd_account t " +
                " where a.pk_account = t.pk_account " +
                "   and a.pk_accchart = '" + pk_accchart + "' " +
                "   and t.code = '" + acountCode + "' " +
                " order by t.code asc";
		return sql;
	}
	
	public BaseDAO getBaseDAO() {
		return baseDAO == null ? new BaseDAO() : baseDAO;
	}
	
}
